package com.gton.router.impl;

import com.gton.entity.GpLogin;
import com.gton.entity.Student;
import com.gton.io.Resp;
import com.gton.router.RemoteUserManageService;
import com.gton.service.GotoUserManagerService;
import com.gton.service.GpLoginService;
import com.gton.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @description: 基于dubbo实现远程过程调用
 * @author: GuoTong
 * @createTime: 2022-09-24 17:40
 * @since JDK 1.8 OR 11
 **/
@Service
@DubboService
@Slf4j
public class DubboRouterService implements RemoteUserManageService {

    @Autowired
    private GotoUserManagerService gotoUserManagerService;

    @Autowired
    private StudentService studentService;

    /**
     * Description: Dubbo的消息提供方  @DubboService
     *
     * @author: GuoTong
     * @date: 2022-09-24 17:42:36
     */

    @Override
    public Resp<Map<String, Object>> getUserManagerService_Hello(String TraceId, String userName) {
        log.info("[{}]dubbo 方式请求进入了！！！！", TraceId);
        Resp<Map<String, Object>> mapData = gotoUserManagerService.createMapData();
        Map<String, Object> data = mapData.getData();
        data.put("TraceId", TraceId);
        data.put("userName", userName);
        return mapData;
    }

    @Override
    public Resp databaseUpdate(Object gpLogin) {
        Student gpLogin2 = null;
        if (gpLogin instanceof GpLogin) {
            Long id = ((GpLogin) gpLogin).getId();
            if (id != null && id > 0) {
                // 插入本地数据库
                gpLogin2 = new Student().
                        setUsername("thirdsysrouter")
                        .setPassword("thirdsysrouter")
                        .setPwdShow("分布式事务thirdsysrouter");
                studentService.save(gpLogin2);
                // 执照异常，利用分布式事务回滚  int i = 100 / 0;
            }
        }
        return gpLogin2 != null ? Resp.Ok(gpLogin2.getId()) : Resp.error("利用分布式事务");
    }
}
